import {
  CreateAppV2Req,
  CreateAppV2Res,
  UpdateAppV2Req,
  UpdateAppV2Res,
  PublishAppV2Req,
  PublishAppV2Res,
  AppReviewHistoryReq,
  AppReviewHistoryRes,
  HotIssueRes,
  UpdateHotIssueReq,
  UpdateInputGuideReq,
  InputGuideRes,
  UploadImageReq,
  GetImageRes,
  AppReviewReq,
  AppReviewRes,
  UpdateAppImageReq,
} from '@/common/types';

import apiClient from '../apiClient';

export enum AppApi {
  CreateAppV2 = '/openapi/v2/apps/create',
  UpdateAppV2 = '/openapi/v2/apps/update',
  AppDetailV2 = '/openapi/v2/apps/detail',
  AppPublishV2 = '/openapi/v2/apps/publish',
  AppReviewHistory = '/openapi/v2/apps/review/history',
  AppHotIssue = '/openapi/v2/apps/hot_issue',
  AppHotIssueUpdate = '/openapi/v2/apps/hotissue/update',
  AppInputGuide = '/openapi/v2/apps/input_guide',
  AppInputGuideUpdate = '/openapi/v2/apps/input_guide/update',
  AppUploadImage = '/openapi/v2/apps/upload_image',
  AppGetImage = '/openapi/v2/apps/get_image',
  AppReview = '/openapi/v2/apps/review',
  AppPublishHistory = '/openapi/v2/apps/publish/history',
  DatasetCount = '/openapi/v2/dataset/count',
  UploadImage = '/api/ruigpt/shared/document/uploadImage',
  UpdateAppImage = '/openapi/v2/apps/update/image',
  TemplateList = '/openapi/v2/prompt/template/list',

  // 验证余额接口
  VerifyBalanceApi = '/openapi/v2/apps/internet_search/check',
}

const createAppV2 = (params: CreateAppV2Req) =>
  apiClient.post<CreateAppV2Res>({
    url: AppApi.CreateAppV2,
    data: params,
  });

const updateAppV2 = (params: UpdateAppV2Req) =>
  apiClient.post<UpdateAppV2Res>({
    url: AppApi.UpdateAppV2,
    data: params,
  });

const getAppDetailV2 = (id: string, appWorkbenchType?: string, versionId?: string) =>
  apiClient.get<UpdateAppV2Req>({
    url: `${AppApi.AppDetailV2}/${id}`,
    params: {
      app_workbench_type: appWorkbenchType,
      app_version_id: versionId,
    },
  });

const appPublishV2 = (params: PublishAppV2Req, id: string) =>
  apiClient.post<PublishAppV2Res>({
    url: `${AppApi.AppPublishV2}/${id}`,
    data: params,
  });

const getAppReviewHistory = (params: AppReviewHistoryReq) =>
  apiClient.get<AppReviewHistoryRes>({
    url: AppApi.AppReviewHistory,
    params,
  });

const getAppHotIssue = (id: string) =>
  apiClient.get<HotIssueRes>({
    url: `${AppApi.AppHotIssue}/${id}`,
  });

const updateAppHotIssue = (id: string, params: UpdateHotIssueReq) =>
  apiClient.post({
    url: `${AppApi.AppHotIssueUpdate}/${id}`,
    data: params,
  });

const getAppInputGuide = (id: string) =>
  apiClient.get<InputGuideRes>({
    url: `${AppApi.AppInputGuide}/${id}`,
  });

const updateAppInputGuide = (id: string, params: UpdateInputGuideReq) =>
  apiClient.post({
    url: `${AppApi.AppInputGuideUpdate}/${id}`,
    data: params,
  });

const uploadAppImage = (id: string, params: UploadImageReq) => {
  const formData = new FormData();
  formData.append('file', params.file);
  return apiClient.post({
    url: `${AppApi.AppUploadImage}/${id}`,
    data: formData,
    params: {
      image_func_type: params.image_func_type,
    },
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
};

const updateAppImage = (id: string, params: UpdateAppImageReq) =>
  apiClient.post({
    url: `${AppApi.UpdateAppImage}/${id}`,
    data: params,
  });

const getAppImage = (id: string) =>
  apiClient.get<GetImageRes>({
    url: `${AppApi.AppGetImage}/${id}`,
  });

/**
 * 审核应用
 * @param id 应用ID
 * @param params 审核参数
 * @returns 审核结果
 */
const reviewApp = (id: string, params: AppReviewReq) =>
  apiClient.post<AppReviewRes>({
    url: `${AppApi.AppReview}/${id}`,
    data: params,
  });

const getAppPublishHistory = (id: string) =>
  apiClient.get({
    url: `${AppApi.AppPublishHistory}/${id}`,
  });

const getDatasetCount = (appId: string) =>
  apiClient.get({
    url: AppApi.DatasetCount,
    params: {
      app_id: appId,
    },
  });
const uploadImage = (type: string, base64: string) =>
  apiClient.post({
    url: AppApi.UploadImage,
    params: {
      type,
      base64,
    },
  });

/**
 * 获取prompt提示词模板列表
 * @param params 搜索参数
 * @returns 模板列表
 */
const getTemplateList = (params: { page: number; pageSize: number; searchText?: string }) =>
  apiClient.get({
    url: AppApi.TemplateList,
    params: {
      ...params,
    },
  });

const verifyBalance = (appId: string) =>
  apiClient.get<boolean>({
    url: AppApi.VerifyBalanceApi,
    params: {
      appId,
    },
  });

export default {
  createAppV2,
  updateAppV2,
  getAppDetailV2,
  appPublishV2,
  getAppReviewHistory,
  getAppHotIssue,
  updateAppHotIssue,
  getAppInputGuide,
  updateAppInputGuide,
  uploadAppImage,
  getAppImage,
  reviewApp,
  getAppPublishHistory,
  getDatasetCount,
  uploadImage,
  updateAppImage,
  getTemplateList,
  verifyBalance,
};
